﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Unis.Framework.Core.Utility;

using KingRoad.EPM.CompletionMaterialsEntity.Model;
using KingRoad.EPM.CompletionMaterialsDAL.BcdProjectFileService;
using KingRoad.EPM.CompletionMaterialsDAL.BcdFileDataListService;


namespace KingRoad.EPM.CompletionMaterialsBLL.UIRules
{
    /// <summary> 
    /// 工程项目与材料类型关系表UIRule 
    /// </summary> 
    public class BcdProjectfileUIRule
    {
        private readonly IBcdProjectfileService _bcdProjectfileService;
        private BcdFiledatalistUIRule _bcdFiledatalistUIRule;

        public BcdProjectfileUIRule() { }

        /// <summary> 
        /// construct function... 
        /// </summary> 
        public BcdProjectfileUIRule(IBcdProjectfileService bcdFilelistService)
        {
            _bcdProjectfileService = bcdFilelistService;
        }

        /// <summary>
        /// 根据条件获取信息
        /// </summary>
        /// <returns></returns>
        public List<BcdProjectfileInfo> GetEntityListBySql(string sql)
        {
            var ret = _bcdProjectfileService.GetEntityList(sql);
            return ret;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertEntity(BcdProjectfileInfo entity)
        {
            var ret = _bcdProjectfileService.InsertEntity(entity);
            return ret > 0;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteEntity(BcdProjectfileInfo info)
        {
            _bcdFiledatalistUIRule = new BcdFiledatalistUIRule(new BcdFiledatalistService());
            //根据 项目proId 类型fid 获取 工程项目与归档类型与案卷目录对应关系表
            List<BcdFiledatalistInfo> fdlList = _bcdFiledatalistUIRule.GetEntityListBySql(" P_ID='" + info.P_ID + "' and Fd_ID='" + info.Fd_ID + "' ");
            foreach (BcdFiledatalistInfo fdlInfo in fdlList)
            {
                //同时删除 工程项目与归档类型与案卷目录对应关系表
                _bcdFiledatalistUIRule.DeleteEntity(fdlInfo);
            }
            var ret = _bcdProjectfileService.DeleteEntity(info);
            return ret > 0;
        }

        /// <summary>
        /// 根据项目id，文档类型id 获取该项目下此文档类型的子类型个数
        /// </summary>
        /// <param name="P_ID">项目id</param>
        /// <param name="Fd_ID">文档类型id</param>
        /// <returns></returns>
        public int GetCountByPIDAndFdID(string P_ID, string Fd_ID)
        {
            var ret = _bcdProjectfileService.GetCountByPIDAndFdID(P_ID, Fd_ID);
            return ret;
        }
    }
}

